package com.cat.binaryTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
 * @create 2025/9/8 21:05
 * @since JDK17
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


public class Solution01 {
    public int[] nodesBetweenCriticalPoints(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode p = head.next, pre = head;
        int idx = 1, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        while (p.next != null) {
            var next = p.next;
            if (p.val > pre.val && p.val > next.val) {
                list.add(idx);
            }

            if (p.val < pre.val && p.val < next.val) {
                list.add(idx);
            }

            idx++;
            pre = p;
            p = next;
        }
        if (list.size() < 2) {
            return new int[]{-1, -1};
        } else {
            for (int i = 1; i < list.size(); i++) {
                min = Math.min(min, list.get(i) - list.get(i - 1));
            }
            max = list.get(list.size() - 1) - list.get(0);
            return new int[]{min, max};
        }
    }
}
